package org.dromara.swimming.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.dromara.swimming.domain.bo.EmailBo;
import org.dromara.swimming.domain.vo.EmailVo;
import org.dromara.swimming.domain.Email;
import org.dromara.swimming.mapper.EmailMapper;
import org.dromara.swimming.service.IEmailService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 邮箱管理Service业务层处理
 *
 * @author Ashufen
 * @date 2024-05-09
 */
@RequiredArgsConstructor
@Service
public class EmailServiceImpl implements IEmailService {

    private final EmailMapper baseMapper;

    /**
     * 查询邮箱管理
     */
    @Override
    public EmailVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询邮箱管理列表
     */
    @Override
    public TableDataInfo<EmailVo> queryPageList(EmailBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<Email> lqw = buildQueryWrapper(bo);
        Page<EmailVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        System.out.println(result.getRecords());
        return TableDataInfo.build(result);
    }
    @Override
    public String listString() {
        List<Email> result = baseMapper.selectAll();
        String message = "";
        for (int i = 0; i < result.size(); i++) {
            if (result.get(i).getStatus() != 0) {
                message += result.get(i).getMail() + ",";
            }
        }
        return message;
    }
    /**
     * 查询邮箱管理列表
     */
    @Override
    public List<EmailVo> queryList(EmailBo bo) {
        LambdaQueryWrapper<Email> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<Email> buildQueryWrapper(EmailBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<Email> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getMail()), Email::getMail, bo.getMail());
        lqw.eq(bo.getStatus() != null, Email::getStatus, bo.getStatus());
        return lqw;
    }

    /**
     * 新增邮箱管理
     */
    @Override
    public Boolean insertByBo(EmailBo bo) {
        Email add = MapstructUtils.convert(bo, Email.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改邮箱管理
     */
    @Override
    public Boolean updateByBo(EmailBo bo) {
        Email update = MapstructUtils.convert(bo, Email.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(Email entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除邮箱管理
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
